package com.ztgf.sys.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.db.utils.RedisUtil;
import com.ztgf.shopping.models.service.shop.IShopInfoService;
import com.ztgf.shopping.models.vo.shop.ShopInfoTypeVO;
import com.ztgf.sms.models.service.IEmailInfoService;
import com.ztgf.sys.common.config.YmlConfig;
import com.ztgf.sys.mapper.SysEmailManageMapper;
import com.ztgf.sys.models.bo.SysEmailManageBO;
import com.ztgf.sys.models.bo.SysEmailManageSaveBO;
import com.ztgf.sys.models.dto.SysEmailManageDTO;
import com.ztgf.sys.models.dto.SysNoticeDTO;
import com.ztgf.sys.models.dto.SysUserDTO;
import com.ztgf.sys.models.service.ISysEmailManageService;
import com.ztgf.sys.models.service.ISysNoticeService;
import com.ztgf.sys.models.vo.SysEmailManageVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.ztgf.common.utils.UtilConstants.getlinkNo;
import static com.ztgf.common.utils.UtilConstants.isNull;

/**
 * author: chenyu.wang
 * date: 2020-07-10 15:23
 * Description: 邮件管理发布表
 */
@DubboService
@Slf4j
public class SysEmailManageServiceImpl extends ServiceImpl<SysEmailManageMapper, SysEmailManageDTO> implements ISysEmailManageService {

    @Resource
    private YmlConfig ymlConfig;

    @DubboReference
    private IEmailInfoService emailInfoService;

    @Autowired
    private RedisUtil redisUtil;

    @DubboReference
    private ISysNoticeService iSysNoticeService;

    @DubboReference
    private IShopInfoService iShopInfoService;


    @Override
    public boolean add(SysEmailManageSaveBO saveBO) {
        SysEmailManageDTO dto = new SysEmailManageDTO();
        dto.setId(ymlConfig.getPkId());//id
        dto.setCreateUserId(saveBO.getUserId());
        dto.setSendObject(saveBO.getSendObject());//选择发送对象;0全部用户，1全部店铺，2全部旗舰店，3全部专卖店，4全部专营店，5全部买家，6筛选
        if (saveBO.getSendObject() == 6) {
            if (StringUtils.isNotBlank(saveBO.getFilterSendObject())) {
                dto.setFilterSendObject(saveBO.getFilterSendObject());//筛选发送对象
            }
        } else {
            dto.setFilterSendObject(saveBO.getSendObject().toString());//筛选发送对象
        }
        dto.setNumber(getlinkNo());//编号
        dto.setMailTitle(saveBO.getMailTitle());//邮件标题
        dto.setMailContent(saveBO.getMailContent());//邮件内容
        // 发送邮件
        emailInfoService.adminMessagePush(saveBO.getSendObject(), saveBO.getMailTitle(),
                saveBO.getMailContent(), saveBO.getFilterSendObject());
        return this.insert(dto);
    }

    @Override
    public Page<SysEmailManageVO> queryAdminEmailList(SysEmailManageBO bo) {
        Page<SysEmailManageVO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        List<SysEmailManageVO> list = baseMapper.queryAdminEmailList(page, bo);
        if (!isNull(list)) {
            for (SysEmailManageVO manageVo : list) {
                if (StringUtils.isNotBlank(manageVo.getFilterSendObject())) {
                    switch (manageVo.getFilterSendObject()) {
                        case "0":
                            manageVo.setRecipientName("全部用户");
                            break;
                        case "1":
                            manageVo.setRecipientName("全部店铺");
                            break;
                        case "2":
                            manageVo.setRecipientName("全部旗舰店");
                            break;
                        case "3":
                            manageVo.setRecipientName("全部专卖店");
                            break;
                        case "4":
                            manageVo.setRecipientName("全部专营店");
                            break;
                        case "5":
                            manageVo.setRecipientName("全部买家");
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public SysEmailManageVO queryById(Long id) {
        SysEmailManageVO vo = baseMapper.queryById(id);
        if (StringUtils.isNotBlank(vo.getFilterSendObject())) {
            switch (vo.getFilterSendObject()) {
                case "0":
                    vo.setRecipientName("全部用户");
                    break;
                case "1":
                    vo.setRecipientName("全部店铺");
                    break;
                case "2":
                    vo.setRecipientName("全部旗舰店");
                    break;
                case "3":
                    vo.setRecipientName("全部专卖店");
                    break;
                case "4":
                    vo.setRecipientName("全部专营店");
                    break;
                case "5":
                    vo.setRecipientName("全部买家");
                    break;
                default:
                    break;
            }
        }
        return vo;
    }

    @Override
    public void saveBatchByType(SysEmailManageSaveBO bo, String sessionId, String source) {
        SysUserDTO sysUserDTO = (SysUserDTO) redisUtil.getSession(sessionId, source);
        SysNoticeDTO sysNoticeDTO = new SysNoticeDTO ();
        Long id = ymlConfig.getPkId();
        sysNoticeDTO.setId (id);
        sysNoticeDTO.setCreateUserId (sysUserDTO.getId());
        sysNoticeDTO.setStatus (1);  //设置状态为保存成功
        sysNoticeDTO.setType (1);    //设置状态为邮件发布
        sysNoticeDTO.setSendType (bo.getSendObject ());    //设置发送状态:0全部用户，1全部店铺(包含子账户)，2全部旗舰店，3全部专卖店，4全部专营店，5全部买家（全部注册用户），6条件筛选发送
        sysNoticeDTO.setContent (bo.getMailContent ());
        sysNoticeDTO.setNumber (getlinkNo());//邮件编号
        sysNoticeDTO.setMailTitle (bo.getMailTitle ());
        sysNoticeDTO.setDelFlag (0); //设置状态为未删除

        List<ShopInfoTypeVO> shopInfoTypeVOList = new ArrayList<> ();
        Integer pageNum = 0;
        Integer pageSize = 100;
        String name = "";
        do{
            shopInfoTypeVOList=iShopInfoService.queryshopInfoByType(bo.getFilterSendObject (),bo.getSendObject (),pageNum,pageSize);
            if (shopInfoTypeVOList != null && shopInfoTypeVOList.size () >0){
                String shopName = "";
                String email = "";
                for (ShopInfoTypeVO shopInfoTypeVO:shopInfoTypeVOList) {
                    email += shopInfoTypeVO.getEmail ()+ ",";
                    if(bo.getSendObject () == 0 || bo.getSendObject () == 5){
                        name += shopInfoTypeVO.getName () + ",";
                        shopName += shopInfoTypeVO.getName () + ",";
                    }else if (bo.getSendObject () == 6){
                        if (bo.getServiceType () == 1){ //条件筛选买家
                            name += shopInfoTypeVO.getName () + ",";
                            shopName += shopInfoTypeVO.getName () + ",";
                        }
                        if (bo.getServiceType () == 2){//条件筛选卖家
                            name += shopInfoTypeVO.getShopName () + ",";
                            shopName += shopInfoTypeVO.getShopName () + ",";
                        }
                    } else {
                        name += shopInfoTypeVO.getShopName () + ",";
                        shopName += shopInfoTypeVO.getShopName () + ",";
                    }
                }
                shopName = shopName.substring (0,shopName.length ()-1);
                email = email.substring (0,email.length ()-1);
                SysEmailManageDTO sysEmailManageDTO = new SysEmailManageDTO ();
                Long sysEmailManageId = ymlConfig.getPkId();
                sysEmailManageDTO.setId (sysEmailManageId);
                sysEmailManageDTO.setCreateUserId (sysUserDTO.getId());
                sysEmailManageDTO.setStatus (1);//发送状态置为失败
                sysEmailManageDTO.setMailTitle (bo.getMailTitle ());
                sysEmailManageDTO.setMailContent (bo.getMailContent ());
                sysEmailManageDTO.setSysNoticeId (sysNoticeDTO.getId ());
                sysEmailManageDTO.setName (shopName);
                sysEmailManageDTO.setEmail (email);

                Integer insert = baseMapper.insert (sysEmailManageDTO);
                log.info ("保存短信发布日志结果=============" + insert);
                pageNum += pageSize;
            }
        }while (shopInfoTypeVOList.size () == pageSize);

        if (bo.getFilterSendObject () != null  && bo.getSendObject ()==6){
            name = name.substring (0,name.length ()-1);
            sysNoticeDTO.setName (name);
            sysNoticeDTO.setShopIds (bo.getFilterSendObject ());
        }
        switch (bo.getSendObject ()){
            case 0:
                sysNoticeDTO.setName ("全部用户");
                break;
            case 1:
                sysNoticeDTO.setName ("全部店铺");
                break;
            case 2:
                sysNoticeDTO.setName ("全部旗舰店");
                break;
            case 3:
                sysNoticeDTO.setName ("全部专卖店");
                break;
            case 4:
                sysNoticeDTO.setName ("全部专营店");
                break;
            case 5:
                sysNoticeDTO.setName ("全部买家");
                break;
        }
        boolean flag = iSysNoticeService.insert (sysNoticeDTO); //保存短信发布记录
        log.info ("保存短信发布记录结果=============" + flag);

    }

    @Override
    public Page<SysEmailManageVO> queryPageListByNoticeId(SysEmailManageBO sysEmailManageBO) {
        Page<SysEmailManageVO> page = new Page<>(sysEmailManageBO.getPageNo(), sysEmailManageBO.getPageSize());
        List<SysEmailManageVO> list = baseMapper.queryPageListByNoticeId(page, sysEmailManageBO);
        page.setRecords(list);
        return page;
    }
}
